package com.dofast.bt.mvp.presenter.basket;

import android.os.Handler;
import android.text.TextUtils;

import com.dofast.bt.base.BaseMvpPresenter;
import com.dofast.bt.bean.AddressBean;
import com.dofast.bt.bean.RegionInfo;
import com.dofast.bt.dao.RegionDAO;
import com.dofast.bt.mvp.view.activity.basket.AddressView;
import com.dofast.bt.util.Helper;
import com.dofast.bt.util.Utility;

import java.util.ArrayList;

/**
 * username:Created by hsj on 2016/12/28 0028.
 * class：
 * 类描述：
 */

public class AddressPresenter extends BaseMvpPresenter<AddressView> implements IAddressPresenter {
    private Handler handler;
    static ArrayList<RegionInfo> item1;

    static ArrayList<ArrayList<RegionInfo>> item2 = new ArrayList<ArrayList<RegionInfo>>();

    static ArrayList<ArrayList<ArrayList<RegionInfo>>> item3 = new ArrayList<ArrayList<ArrayList<RegionInfo>>>();

    @Override
    public void initData() {
        checkViewAttach();
        handler = mvpView.getHandle();
        handleData();
    }

    @Override
    public void resetData() {
        mvpView.resetData(item1, item2, item3);
    }

    @Override
    public void getAddress(int province, int city, int country) {
        if (!TextUtils.isEmpty(item1.get(province).getPickerViewText())) {
            mvpView.setProvince(item1.get(province).getPickerViewText());
        } else {
            mvpView.setProvince("");
        }
        if (!TextUtils.isEmpty(item2.get(province).get(city).getPickerViewText())) {
            mvpView.setCity(item2.get(province).get(city).getPickerViewText());
        } else {
            mvpView.setCity("");
        }
        if (!TextUtils.isEmpty(item3.get(province).get(city).get(country).getPickerViewText())) {
            mvpView.setCountry(item3.get(province).get(city).get(country).getPickerViewText());
        } else {
            mvpView.setCountry("");
        }
    }

    /**
     * 保存地址逻辑处理
     */
    @Override
    public void save() {
        if (!isEmpty()) {
            Helper.showToast("保存成功");
            //跳转到地址列表界面
            AddressBean addressBean = new AddressBean();
            addressBean.setName(mvpView.getName());
            addressBean.setPhone(mvpView.getPhone());
            addressBean.setAddress(mvpView.getProvince()+mvpView.getCity()+mvpView.getCountry()+mvpView.getAddress());
            mvpView.gotoEditAddress(addressBean);
            // TODO: 2016/12/28 0028 新增地址  添加入数据库

        }
    }

    private boolean isEmpty() {
        if (TextUtils.isEmpty(mvpView.getName())) {
            Helper.showToast("请输入收货人姓名");
            return true;
        }
        if (TextUtils.isEmpty(mvpView.getPhone())) {
            Helper.showToast("请输入手机号");
            return true;
        }
        if (!Utility.isMobileNO(mvpView.getPhone())) {
            Helper.showToast("您输入的手机号有误，请重新输入！");
            return true;
        }
        if (TextUtils.isEmpty(mvpView.getProvince())) {
            Helper.showToast("请选中省份");
            return true;
        }
        if (TextUtils.isEmpty(mvpView.getCity())) {
            Helper.showToast("请选择城市");
            return true;
        }
        if (TextUtils.isEmpty(mvpView.getCountry())) {
            Helper.showToast("请选择地区");
            return true;
        }
        if (TextUtils.isEmpty(mvpView.getAddress())) {
            Helper.showToast("请输入详细地址");
            return true;
        }
        return false;
    }

    private void handleData() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                System.out.println(System.currentTimeMillis());
                if (item1 != null && item2 != null && item3 != null) {
                    handler.sendEmptyMessage(0x123);
                    return;
                }
                item1 = (ArrayList<RegionInfo>) RegionDAO.getProvencesOrCity(1);
                for (RegionInfo regionInfo : item1) {
                    item2.add((ArrayList<RegionInfo>) RegionDAO.getProvencesOrCityOnParent(regionInfo.getId()));

                }

                for (ArrayList<RegionInfo> arrayList : item2) {
                    ArrayList<ArrayList<RegionInfo>> list2 = new ArrayList<ArrayList<RegionInfo>>();
                    for (RegionInfo regionInfo : arrayList) {


                        ArrayList<RegionInfo> q = (ArrayList<RegionInfo>) RegionDAO.getProvencesOrCityOnParent(regionInfo.getId());
                        list2.add(q);

                    }
                    item3.add(list2);
                }

                handler.sendEmptyMessage(0x123);

            }
        }).start();
    }
}
